package com.jiangyg.mall.member.controller;

import com.jiangyg.mall.core.support.restful.Pagination;
import com.jiangyg.mall.core.support.restful.Result;
import com.jiangyg.mall.member.dto.MemberLevelDTO;
import com.jiangyg.mall.member.service.MemberLevelService;
import com.jiangyg.mall.member.vo.MemberLevelVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 类描述：会员等级控制器
 *
 * @author jiangyg
 * @version email@jiangyg.com
 * @date 2021-10-30 07:33:09
 */
@RestController
@RequestMapping("/member/level")
public class MemberLevelController {

    /**
     * 会员等级接口
     */
    private final MemberLevelService memberLevelService;

    @Autowired
    public MemberLevelController(MemberLevelService memberLevelService) {
        this.memberLevelService = memberLevelService;
    }

    /**
     * 功能描述：会员等级分页查询
     *
     * @param request    请求
     * @param pagination 分页参数
     * @return 返回分页会员等级
     */
    @ResponseBody
    @PostMapping("/select")
    public Result<Pagination<MemberLevelVO>> select(HttpServletRequest request, @RequestBody Pagination<MemberLevelVO> pagination) throws Exception {
        return Result.ok(memberLevelService.selectPagination(pagination));
    }

    /**
     * 功能描述：会员等级列表查询
     *
     * @param request 请求
     * @return 返回会员等级
     */
    @ResponseBody
    @PostMapping("/list")
    public Result<List<MemberLevelDTO>> list(HttpServletRequest request) throws Exception {
        return Result.ok(memberLevelService.selectList());
    }

    /**
     * 功能描述：保存或者修改会员等级
     *
     * @param request 请求
     * @param dto     会员等级
     * @return 结果
     */
    @ResponseBody
    @PostMapping("/save")
    public Result<Object> save(HttpServletRequest request, @RequestBody @Validated MemberLevelDTO dto) throws Exception {
        return Result.saveOrUpdate(memberLevelService.saveOrUpdate(dto));
    }

    /**
     * 功能描述：根据ID获取会员等级
     *
     * @param request       请求
     * @param memberLevelId 会员等级ID
     * @return 结果
     */
    @ResponseBody
    @PostMapping("/info")
    public Result<MemberLevelVO> info(HttpServletRequest request, @RequestParam(name = "levelId") Long memberLevelId) throws Exception {
        return Result.ok(memberLevelService.selectById(memberLevelId));
    }

    /**
     * 功能描述：删除会员等级
     *
     * @param request        请求
     * @param memberLevelIds 批量删除的会员等级ID
     * @return 结果
     */
    @ResponseBody
    @PostMapping("/delete")
    public Result<Object> delete(HttpServletRequest request, @RequestBody List<Long> memberLevelIds) {
        return Result.delete(memberLevelService.delete(memberLevelIds));
    }

}
